package com.sinodata.bsm.inmp.task.gab;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.sinodata.bsm.common.vo.Event;

/**
 * <p>
 * Description: 此类是用于温湿度集成事件的缓存类
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2013-8-20 下午4:35:04          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class NewEventCache {
    private static final NewEventCache instance = new NewEventCache();
    private final Map<String, Event> newEventCache = new HashMap<String, Event>();
    private final Map<Long, Map<String, String>> keyMap = new HashMap<Long, Map<String, String>>();
    private final Set<String> confirmedEventSuffixSet = new HashSet<String>();

    private NewEventCache() {
        init();
    }

    private void init() {
        confirmedEventSuffixSet.add("恢复正常");
        confirmedEventSuffixSet.add("解除");

        Map<String, String> ktMap = new HashMap<String, String>();
        ktMap.put("恢复正常", "过高");
        ktMap.put("报警解除", "报警发生");
        ktMap.put("解除", "报警");
        keyMap.put(6021000202000L, ktMap);

        Map<String, String> upsMap = new HashMap<String, String>();
        upsMap.put("通讯恢复正常", "通讯错");
        upsMap.put("百分数恢复正常", "百分数越低限");
        upsMap.put("电压恢复正常", "电压越高限");
        upsMap.put("解除", "报警");
        upsMap.put("报警解除", "报警发生");
        upsMap.put("频率恢复正常", "频率越低限");
        keyMap.put(6021000203000L, upsMap);

        Map<String, String> dlyMap = new HashMap<String, String>();
        dlyMap.put("恢复正常", "故障");
        keyMap.put(6021000204000L, dlyMap);

        Map<String, String> wsdMap = new HashMap<String, String>();
        wsdMap.put("恢复正常", "故障");
        keyMap.put(6021000204000L, wsdMap);
    }

    public static NewEventCache getCache() {
        return instance;
    }

    public void put(Event event) {
        if (event.getStatus() == 0 && event.getObjectId() != null) {
            newEventCache.put(event.getObjectId(), event);
        }
    }

    public Event get(Long resId, String event) {
        String objectId = getKey(resId, event);
        Event e = newEventCache.get(objectId);
        if (e != null) {
            newEventCache.remove(e.getObjectId());
        }
        return e;
    }

    private String getKey(Long resId, String event) {
        String objectId = null;
        Set<String> keySet = keyMap.get(resId).keySet();
        for (String key : keySet) {
            if (event.endsWith("报警解除")) {
                objectId = event.replace("报警解除", keyMap.get(resId).get("报警解除"));
            } else if (event.endsWith(key)) {
                objectId = event.replace(key, keyMap.get(resId).get(key));
            }
        }
        return objectId;
    }

    public boolean isConfirmedEvent(String event) {
        for (String suffix : confirmedEventSuffixSet) {
            if (event.endsWith(suffix)) {
                return true;
            }
        }
        return false;
    }
}
